package com.ericom.webapp.action;

import java.util.List;

import com.ericom.model.VariationType;
import com.ericom.service.GenericManager;

public class VariationTypeAction extends BaseAction {
	private GenericManager<VariationType, Long> variationTypeManager;
	private List variationTypes;
	private VariationType variationType;
	private Long id;

	public VariationType getVariationType() {
		return variationType;
	}

	public void setVariationType(VariationType variationType) {
		this.variationType = variationType;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setVariationTypeManager(
			GenericManager<VariationType, Long> variationTypeManager) {
		this.variationTypeManager = variationTypeManager;
	}

	public String list() {
		variationTypes = variationTypeManager.getAll();
		return SUCCESS;
	}

	public List getVariationTypes() {
		return variationTypes;
	}

	public String delete() {
		variationTypeManager.remove(variationType.getId());
		this.saveMessage(getText("variationType.deleted"));
		return this.SUCCESS;
	}

	public String edit() {
		if (id != null) {
			variationType = variationTypeManager.get(id);
		} else {
			variationType = new VariationType();
		}
		return this.SUCCESS;
	}

	public String save() throws Exception {
		if (this.cancel != null) {
			return this.CANCEL;
		}
		if (this.delete != null) {
			return this.delete();
		}

		boolean isNew = (variationType.getId() == null);
		variationType = variationTypeManager.save(variationType);

		String key = (isNew) ? "variationType.added" : "variationType.updated";
		this.saveMessage(getText(key));

		if (!isNew) {
			return this.INPUT;
		} else {
			return this.SUCCESS;
		}
	}
}
